package com.ndp.fb.walle.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.ec.core.FeedService;
import com.ndp.ec.core.model.SamplingFeed;
import com.ndp.fb.mongo.model.Feed;
import com.ndp.fb.redis.api.RedisClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hunk.qin on 2016/4/13.
 */
@RestController
@RequestMapping("/productview")
public class ProductViewController {

    @Autowired
    private RedisClient redisClient;
    @Reference
    private FeedService feedService;
    @RequestMapping("originalShow")
    public List<SamplingFeed> samplingOriginalProduct(@RequestParam(value = "feedId", required = true) Long  feedId){

        List<SamplingFeed> list = getSamplingFeeds(feedId);
        return list;
    }


    @RequestMapping("materialShow")
    public List<SamplingFeed> samplingMaterialShowProduct(@RequestParam(value = "feedId", required = true) Long  feedId,
                                                      @RequestParam(value = "material", required = true)  Long material){
        List<SamplingFeed> list = getSamplingFeeds(feedId);
        return list;
    }


    @RequestMapping("materialShowItem")
    public Feed samplingMaterialShowProductItem(@RequestParam(value = "feedId", required = true) Long  feedId,
                                                @RequestParam(value = "material", required = true)  Long material,
                                                @RequestParam(value = "itemId", required = true)  String itemId){
        return getFeed(feedId,itemId);
    }

    @RequestMapping("originalShowItem")
    public Feed samplingOriginalProductItem(@RequestParam(value = "feedId", required = true) Long  feedId,
                                            @RequestParam(value = "itemId", required = true)  String itemId){
        return getFeed(feedId,itemId);
    }

    private Feed getFeed(Long feedId,String itemId) {
    	return feedService.getFeedItem(feedId, itemId);
    }

    private List<SamplingFeed> getSamplingFeeds(Long feedId) {
        List<SamplingFeed> list=new ArrayList<>();
        List<Feed> feedList=(List<Feed>) redisClient.get("feedId_"+feedId);
        if(feedList!=null){
            for (Feed feed : feedList) {
                SamplingFeed sf=new SamplingFeed();
                BeanUtils.copyProperties(feed,sf);
                list.add(sf);
            }
        }
        return list;
    }


}
